#include "GameWindow.hh"
#include "Bolygo.hh"
#include "Flotta.hh"
#include "GameControls.hh"
#include "Hatter.hh"
#include "PlanetControls.hh"
#include "PlanetToolbar.hh"
#include "../Network/StateChanged.hh"

using namespace Gui;
using namespace Network;

GameWindow::GameWindow(QWidget *parent):
    QMainWindow(parent),
    hatter(0),
    selectedFleet(0),
    selectedStar(0)
{
	ui.setupUi(this);
	QScrollArea* scrollArea = new QScrollArea(this);
	QDockWidget* dockWidget = new QDockWidget(this);
	QDockWidget* leftDock = new QDockWidget(this);
    hatter = new Hatter(scrollArea);
	GameControls* gameControls = new GameControls(dockWidget);
	PlanetControls* planetControls = new PlanetControls(leftDock);
	setCentralWidget(scrollArea);
	addDockWidget(Qt::BottomDockWidgetArea, dockWidget);
	addDockWidget(Qt::LeftDockWidgetArea, leftDock);
	scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scrollArea->setWidget(hatter);
	dockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
	dockWidget->setWidget(gameControls);
	dockWidget->setMinimumHeight(190);
	leftDock->setFeatures(QDockWidget::NoDockWidgetFeatures);
	leftDock->setWidget(planetControls);
	leftDock->setMinimumWidth(260);
	connect(gameControls, SIGNAL(next_turn_clicked()), this, SLOT(ujKor()));
	connect(gameControls, SIGNAL(exit_clicked()), this, SLOT(close()));
	connect(
      gameControls, SIGNAL(chat_send_clicked(QString)),
      this,         SLOT(chat_send_clicked(QString))
	);

	QDockWidget* dockWidget2 = new QDockWidget(this);
	PlanetToolbar* planetToolbar = new PlanetToolbar(dockWidget2);
	addDockWidget(Qt::RightDockWidgetArea, dockWidget2);
	this->planetToolbar = planetToolbar;
    connect(
      planetToolbar, SIGNAL(planet_targeted(u8, Bolygo*)),
      this,          SLOT(planet_targeted(u8, Bolygo*))
    );
    connect(
      planetToolbar,  SIGNAL(planet_selected_sig(Bolygo*)),
      planetControls, SLOT(update_planet(Bolygo*))
    );
    connect(
      planetControls, SIGNAL(build(Bolygo*, QString, u8)),
      this,           SLOT(emit_build(Bolygo*, QString, u8))
    );
	dockWidget2->setMinimumWidth(120);
	dockWidget2->setFeatures(QDockWidget::NoDockWidgetFeatures);
}

GameWindow::~GameWindow() {}

void GameWindow::ujKor()
{
	emit korvaltas();
}

void GameWindow::clear() {
    QList< Flotta*  > flottak   = findChildren< Flotta*  >();
    QList< Csillag* > csillagok = findChildren< Csillag* >();
    PlanetToolbar* planetToolbar = findChild< PlanetToolbar* >();
    PlanetControls* planetControls = findChild< PlanetControls* >();
    selectedFleet = 0;
    selectedStar = 0;
    planetToolbar->clear();
    planetControls->clear();

    for (
      QList< Flotta* >::iterator flotta = flottak.begin();
      flotta != flottak.end();
      ++flotta
    ) {
        delete *flotta;
    }

    for (
      QList< Csillag* >::iterator csillag = csillagok.begin();
      csillag != csillagok.end();
      ++csillag
    ) {
        delete *csillag;
    }
}

void GameWindow::show_fleet(Network::StateChanged::FleetInfo fi, QString s) {
	Flotta* f = new Flotta(hatter, fi, s);
	connect(
      f,    SIGNAL(left_clicked(Flotta*)),
      this, SLOT(flotta_kival(Flotta*))
    );
	connect(
	  f,    SIGNAL(right_clicked(Flotta*)),
	  this, SLOT(flotta_celpont(Flotta*))
	);
	f->show();
}

void GameWindow::show_star(StateChanged::StarInfo const& si) {
    Csillag* cs = new Csillag(hatter, si);
    cs->setObjectName(QString::number(si.id));
    cs->show();
    connect(
    		cs, 	SIGNAL(left_clicked(Csillag*)),
    		this,	SLOT(star_clicked_left(Csillag*))
    );
}

void GameWindow::flotta_kival(Flotta* flotta) {
    if (selectedFleet == flotta)
        return;

	if (selectedFleet)
		selectedFleet->deselect();

	selectedFleet = flotta;

	if (selectedFleet)
	    selectedFleet->select();
}

void GameWindow::flotta_celpont(Flotta* flotta) {
    if (!selectedFleet || !flotta || selectedFleet == flotta)
        return;

    if (selectedFleet->get_player() == flotta->get_player())
        return;

    emit attack_fleet(selectedFleet, flotta);
}

Flotta* GameWindow::get_selected_fleet() const {
	return selectedFleet;
}

void GameWindow::chat_msg_received(QString sender, QString msg) {
	QPlainTextEdit* chatMessages = findChild<QPlainTextEdit*>("chatMessages");
	chatMessages->appendPlainText(sender + QString(": ") + msg);
}

void GameWindow::chat_send_clicked(QString msg) {
	emit send_chat_msg_to_server(msg);
}

void GameWindow::planet_targeted(u8 starId, Bolygo* planet) {
    qDebug("slot! GameWindow::planet_targeted");

    if (!selectedFleet || !planet)
        return;

    if (selectedFleet->get_player() == planet->get_player())
        return;

    if (planet->get_player() == "") {
        qDebug("signal! GameWindow::colonize_planet");
        emit colonize_planet(selectedFleet, starId, planet);
    } else {
        qDebug("signal! GameWindow::attack_planet");
        emit attack_planet(selectedFleet, starId, planet);
    }
}

void GameWindow::emit_build(Bolygo* planet, QString what, u8 count) {
    qDebug("slot! GameWindow::emit_build");

    if (!selectedStar || !planet || !count)
        return;

    qDebug("signal! GameWindow::build");
    emit build(selectedStar, planet, what, count);
}

void GameWindow::star_clicked_left(Csillag* star) {
	planetToolbar->showStar(star);
	planetToolbar->show();
    selectedStar = star;

}
